#include "stdafx.h"
#include "config/IncludeParts.h"
#include "config/Def.h"

#if RENDER_GL

#include "GLRender.h"
#include "config/EngineCon.h"

using namespace unigame;

void GLRender::init()
{
	glClearColor(CLEAR_COLOR_R, CLEAR_COLOR_G, CLEAR_COLOR_B, 1);
	glClearDepth(1);
	glDepthFunc(GL_LEQUAL);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glColor4f(DEFAULT_COLOR_R, DEFAULT_COLOR_G, DEFAULT_COLOR_B, DEFAULT_COLOR_A);
}

void GLRender::enableDepth(bool isEnable)
{
	if(isEnable)
	{
		glEnable(GL_DEPTH);
	}
	else
	{
		glDisable(GL_DEPTH);
	}
}

void GLRender::setProjectMatrix(const Matrix4x4f& m)
{
	glMatrixMode(GL_PROJECTION);
	glLoadMatrixf(m.value());
}

void GLRender::setViewMatrix(const Matrixf& m)
{
	glMatrixMode(GL_MODELVIEW);
	float temp[16];
	int32u len = 3 * sizeof(float);
	memcpy(temp, m.value(), len);
	memcpy(temp + 4, m.value() + 3, len);
	memcpy(temp + 8, m.value() + 6, len);
	memcpy(temp + 12, m.value() + 9, len);
	temp[3] = 0;
	temp[7] = 0;
	temp[11] = 0;
	temp[15] = 1;
	glLoadMatrixf(temp);
}

void GLRender::setViewPort(float x, float y, float w, float h)
{
	glViewport((int32u)x, (int32u)y, (int32u)w, (int32u)h);
}

void GLRender::renderStart()
{
#if UNI_WIN32
	wglMakeCurrent(m_hdc, m_glrc);
#endif
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

void GLRender::renderEnd()
{
#if UNI_WIN32
	SwapBuffers(m_hdc);
	wglMakeCurrent(0, 0);
#endif
}

void GLRender::deleteContext()
{
#if UNI_WIN32
	wglDeleteContext(m_glrc);
#endif
}

void GLRender::setContext(void* hdc)
{
#if UNI_WIN32
	m_hdc = (HDC)hdc;

	PIXELFORMATDESCRIPTOR pfd;
	pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
	pfd.nVersion = 1;
	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits = 24;
	pfd.cRedBits = 0;
	pfd.cRedShift = 0;
	pfd.cGreenBits = 0;
	pfd.cGreenShift = 0;
	pfd.cBlueBits = 0;
	pfd.cBlueShift = 0;
	pfd.cAlphaBits = 0;
	pfd.cAlphaShift = 0;
	pfd.cAccumBits = 0;
	pfd.cAccumRedBits = 0;
	pfd.cAccumGreenBits = 0;
	pfd.cAccumBlueBits = 0;
	pfd.cAccumAlphaBits = 0;
	pfd.cDepthBits = 32;
	pfd.cStencilBits = 0;
	pfd.cAuxBuffers = 0;
	pfd.iLayerType = PFD_MAIN_PLANE;
	pfd.bReserved = 0;
    pfd.dwLayerMask = 0;
    pfd.dwVisibleMask = 0;
    pfd.dwDamageMask = 0;

	int pixelFormat = ChoosePixelFormat(m_hdc, &pfd);
	SetPixelFormat(m_hdc, pixelFormat, &pfd);
	m_glrc = wglCreateContext(m_hdc);
	assert(m_glrc && "gl rc fail");
#endif
}

void GLRender::setColor(float r, float g, float b, float a)
{
	glColor4f(r, g, b, a);
}

void GLRender::test()
{				
	/*glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	 gluPerspective(60.0f, 320.0f / 480,0.3f,1000.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0 , 0, -3, 0, 0, 1, 0, 1, 0);*/
	
	glBegin(GL_QUADS);						
	glVertex3f( -0.5f, 0.5f, 0);				
	glVertex3f(-0.5f,-0.5f,0);		
	glVertex3f( 0.5f,-0.5f,0);		
	glVertex3f(0.5f, 0.5f, 0);
	glEnd();
}

#endif